package com.ruoyi.media.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.media.domain.Images;
import com.ruoyi.media.domain.dto.ImagesDTO;
import com.ruoyi.media.domain.vo.ImageVO;
import com.ruoyi.media.service.DataFileService;
import com.ruoyi.media.service.IImagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/display/images")
public class DisplayImageController extends BaseController {



    @Autowired
    private DataFileService dataFileService;

    @Autowired
    private IImagesService imagesService;

     /**
     * 查询图片管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ImagesDTO imagesDTO)
    {
        startPage();

        List<ImageVO> list = imagesService.imagesList(imagesDTO);

        return getDataTable(list);
    }

    /**
     * 获取图片管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        Images images = imagesService.selectImagesById(id);
        return success(images);
    }


    /**
     * 图片上传
     * @return
     */
    @PostMapping("/upload/{categoryId}/{tagIds}")
    public AjaxResult upload(
            @RequestParam("file") MultipartFile file,
            @PathVariable(value = "tagIds",required = false) ArrayList<Long> tagIds,
            @PathVariable("categoryId") Long categoryId

    ){
        ImagesDTO imagesDTO = new ImagesDTO();
        imagesDTO.setTagIds(tagIds);
        imagesDTO.setCategoryId(categoryId);

        String pathUrl = dataFileService.uploadImgFile(file, imagesDTO);

        if(!StringUtils.isEmpty(pathUrl))
            return success(pathUrl);
        else
            return error("文件上传失败");
    }

    /**
     * 图片下载
     * @return
     */
    @GetMapping("/download/{id}")
    public void download(@PathVariable("id")Long id, HttpServletResponse response){

         dataFileService.imageDownload(id,response);
    }

}
